package com.mz.mattress.admin.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.BlackQueryReq;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.BlackService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Black;
import com.mz.mattress.api.jwt.MerchantJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "black")
public class BlackController {

    @Autowired
    private BlackService blackService;

    @GetMapping("/listPage")
    public Result<IPage<Black>> listPage(BlackQueryReq req) {

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        log.info("typeId={}", userJwt.getTypeId());
        req.setMerchantId(userJwt.getTypeId());

        return blackService.listPage(req);
    }

    @GetMapping("/{id}")
    public Result<Black> detail(@PathVariable Integer id) {
        return blackService.getById(id);
    }

    @PostMapping
    public Result add(@RequestBody Black black) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        black.setMerchantId(userJwt.getTypeId());
        return blackService.add(black);
    }

//    @PostMapping(value = "/itemSave")
//    public Result<SaveDeviceResp> itemSave(@RequestBody BlackSaveReq req) {
//        return blackService.itemSave(req);
//    }

    @PutMapping
    public Result update(@RequestBody Black black) {

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        black.setMerchantId(userJwt.getTypeId());

        if (!black.getMerchantId().equals(userJwt.getTypeId())) {
            return Result.fail("不能修改");
        }

        return blackService.updateById(black);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return blackService.deleteByIds(ids);
    }

}
